/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.util.Vector;
import view.MessageForm;

/**
 *
 * @author Kvest
 */
public class MessageFormPool 
{
    private static MessageFormPool pool = null;
    private static final Object lock = new Object();
    
    public static MessageFormPool getPool()
    {
        synchronized(lock)
        {
            if (pool == null)
            {
                pool = new MessageFormPool();
            }
        }
        
        return pool;
    }
    
    private Vector items;
    
    private MessageFormPool()
    {
        items = new Vector();
    }
    
    public MessageForm getMessageForm()
    {
        synchronized(items)
        {
            if (!items.isEmpty())
            {
                //Забираем форму из коллекции
                MessageForm result = (MessageForm)items.lastElement();
                items.removeElement(result);
                
                //Возвращаем форму
                return result;
            }
        }
        
        return new MessageForm();
    }
    
    public void dispose(MessageForm form)
    {
        synchronized(items)
        {
            if (!items.contains(form))
            {
                items.addElement(form);
            }
        }
    }
}
